//
//  TxLivePlatFormView.m
//  tx_live_plugin
//
//  Created by FXY on 2022/6/26.
//

#import "TxLivePlatFormView.h"

#import "TxLivePushView.h"
#import "TxLivePlayView.h"

typedef enum : NSUInteger {
    TxLivePlatFormPush = 0,  // 直播推流
    TxLivePlatFormPlay  // 播放
} TxLivePlatForm;

@interface TxLivePlatFormView ()

@property (assign, nonatomic) TxLivePlatForm livePlatForm;

@property (strong,nonatomic) TxLivePushView *pushView;

@property (strong,nonatomic) TxLivePlayView *playView;

@end

@implementation TxLivePlatFormView

- (void)dealloc{
    NSLog(@"TxLivePlatFormView dealloc");
}

- (instancetype)initWithFrame:(CGRect)frame viewId:(int64_t)viewId arguments:(id)args binaryMessenger:(NSObject<FlutterBinaryMessenger> *)message{
    
    NSString *type = @"tx_live_push";
    
    if (args!=nil && [args isKindOfClass:NSDictionary.class]) {
        NSDictionary *dic = (NSDictionary *)args;
        if ([dic.allKeys containsObject:@"tx_live_view_type"]) {
            type = [dic valueForKey:@"tx_live_view_type"];
        }
    }
    
    if ([type isEqualToString:@"tx_live_play"]) {
        self.livePlatForm = TxLivePlatFormPlay;
    } else {
        self.livePlatForm = TxLivePlatFormPush;
    }
      
    //channel
    NSString* name = [NSString stringWithFormat:@"com.notary.tx_live_plugin/%@_%lld",type,viewId];
    self.channel = [FlutterMethodChannel methodChannelWithName:name binaryMessenger:message];
    //处理channel回调
    __weak __typeof__(self) weakSelf = self;
    [self.channel setMethodCallHandler:^(FlutterMethodCall * _Nonnull call, FlutterResult  _Nonnull result) {
        if (weakSelf.livePlatForm == TxLivePlatFormPlay) {
            [weakSelf playHandleMethodCall:call result:result];
        } else {
            [weakSelf pushHandleMethodCall:call result:result];
        }
        
    }];
    
    if (self.livePlatForm == TxLivePlatFormPlay) {
        self.playView = [[TxLivePlayView alloc] initWithFrame:frame args:args];
        
        self.playView.callBlock = ^(NSString * _Nonnull method, id  _Nonnull arguments) {
            [weakSelf.channel invokeMethod:method arguments:arguments];
        };
    } else {
        self.pushView = [[TxLivePushView alloc] initWithFrame:frame args:args];
    }
    
    return self;
}

- (nonnull UIView *)view {
    if (self.livePlatForm == TxLivePlatFormPush) {
        return self.pushView;
    } else if (self.livePlatForm == TxLivePlatFormPlay) {
        return self.playView;
    }
    return UIView.new;
}

///处理flutter发送来的消息
- (void)pushHandleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
  if ([@"getPlatformVersion" isEqualToString:call.method]) {
    result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]);
  } else if([@"startLivePush" isEqualToString:call.method]){
      [self.pushView startLivePush:call.arguments];
      if (result) {
          result([NSNumber numberWithBool:YES]);
      }
  } else if([@"stopLivePush" isEqualToString:call.method]){
      [self.pushView stopLivePush];
      if (result) {
          result([NSNumber numberWithBool:YES]);
      }
  } else if([@"onPause" isEqualToString:call.method]){
      [self.pushView onPause];
      if (result) {
          result([NSNumber numberWithBool:YES]);
      }
  } else if([@"onResume" isEqualToString:call.method]){
      [self.pushView onResume];
      if (result) {
          result([NSNumber numberWithBool:YES]);
      }
  } else if([@"onDestroy" isEqualToString:call.method]){
      [self.pushView onDestroy];
      if (result) {
          result([NSNumber numberWithBool:YES]);
      }
  } else if([@"isPushRecording" isEqualToString:call.method]){
      if (result) {
          result([NSNumber numberWithBool:[self.pushView isPushRecording]]);
      }
  } else if([@"turnOnFlashLight" isEqualToString:call.method]){
      [self.pushView turnOnFlashLight];
      if (result) {
          result([NSNumber numberWithBool:YES]);
      }
  } else if([@"isFlashLightOpen" isEqualToString:call.method]){
      if (result) {
          result([NSNumber numberWithBool:[self.pushView isFlashLightOpen]]);
      }
  } else if([@"isFontCamera" isEqualToString:call.method]){
      if (result) {
          result([NSNumber numberWithBool:[self.pushView isFontCamera]]);
      }
  } else if([@"switchCamera" isEqualToString:call.method]){
      [self.pushView switchCamera];
      if (result) {
          result([NSNumber numberWithBool:YES]);
      }
  } else {
      if (result) {
          result(FlutterMethodNotImplemented);
      }
  }
}

///处理flutter发送来的消息
- (void)playHandleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
    
  if ([@"getPlatformVersion" isEqualToString:call.method]) {
    result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]);
  } else if([@"startPlay" isEqualToString:call.method]){
      [self.playView startPlay:call.arguments];
      if (result) {
          result([NSNumber numberWithBool:YES]);
      }
  } else if([@"stopPlay" isEqualToString:call.method]){
      [self.playView stopPlay];
      if (result) {
          result([NSNumber numberWithBool:YES]);
      }
  } else if([@"onPause" isEqualToString:call.method]){
      [self.playView onPause];
      if (result) {
          result([NSNumber numberWithBool:YES]);
      }
  } else if([@"onResume" isEqualToString:call.method]){
      [self.playView onResume];
      if (result) {
          result([NSNumber numberWithBool:YES]);
      }
  } else if([@"onDestroy" isEqualToString:call.method]){
      [self.playView onDestroy];
      if (result) {
          result([NSNumber numberWithBool:YES]);
      }
  } else if([@"firstPlayRtmp" isEqualToString:call.method]){
      [self.playView firstPlayRtmp];
      if (result) {
          result([NSNumber numberWithBool:YES]);
      }
  } else if([@"isPlaying" isEqualToString:call.method]){
      
      if (result) {
          result([NSNumber numberWithBool:[self.playView isPlaying]]);
      }
  } else if([@"seek" isEqualToString:call.method]){
      [self.playView seek:call.arguments];
      if (result) {
          result([NSNumber numberWithBool:YES]);
      }
  } else {
      if (result) {
          result(FlutterMethodNotImplemented);
      }
  }
    
}

@end
